/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.dao.implement;

import ar.com.gruposicom.dao.OperationDao;
import ar.com.gruposicom.domain.Operation;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marcelo
 */
public class OperationDaoImp extends DataManager implements OperationDao 
{

    private Statement stmt;
    private Connection conn;
    
    @Override
    public int save(Operation o) 
    {
        int id = 0;
        
            
            ResultSet resu = null;
            try 
            { 
                conn = super.getConection();
                stmt = conn.createStatement();
                stmt.executeUpdate("INSERT INTO operaciones (nombre) VALUES ("
                +"'"+o.getName().trim()+"')");  
                resu = stmt.executeQuery("Select MAX(operacion_id) From operaciones");
                
            }
            catch (SQLException ex)
            {
              while (ex !=null)
                {
                ex.printStackTrace();
                ex = ex.getNextException();
                }
            }
        try 
        {
            resu.next();
            id = resu.getInt(1);
            this.cerrar();
        } catch (SQLException ex) {
            Logger.getLogger(OperationDaoImp.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    @Override
    public Collection<Operation> getAll() 
    {
        ResultSet resu = null;
        try 
        { 
            conn = super.getConection();   
            if(conn == null)
                return null;
            else
            {
                stmt = conn.createStatement();  
                resu = stmt.executeQuery("SELECT operacion_id, nombre "
                    + "FROM operaciones");
            }
        }
        catch (SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
            return null;
        } 
        Collection<Operation> co = new ArrayList<>();   
        try
        {
            while (resu.next())
            {
                Operation o = new Operation();
                o.setId(resu.getInt(1));
                o.setName(resu.getString(2));
                co.add(o);
            }
        }
        catch(SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        this.cerrar();            
        return co; 
    }

    @Override
    public Operation get(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Operation o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modify(Operation o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Operation> getAllFromUser(int idUser) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void cerrar()
    {
      try
      {
        stmt.close();
        conn.close();
      }
      catch(SQLException e)
      {
        while (e != null) 
        {
          e.printStackTrace();
          e = e.getNextException();
        }        
      }      
    }
    
}
